package work.tree;

import work.TreeNode;

import java.util.*;

public class Problem102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queuqe = new LinkedList<>();
        queuqe.add(root);
        List<List<Integer>> ans = new ArrayList<>();
        while (!queuqe.isEmpty()) {
            int size = queuqe.size();
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queuqe.poll();
                if (poll == null) continue;
                list.add(poll.val);
                queuqe.add(poll.left);
                queuqe.add(poll.right);
            }
            if (list.size() > 0) ans.add(list);
        }
        return ans;
    }



    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode();
        Integer[] nums = new Integer[]{3, 9, 20, null, null, 15, 7};
        TreeNode treeNode1 = treeNode.constructTree(nums);
        treeNode1.printTree();
        Problem102 problem102 = new Problem102();
        List<List<Integer>> lists = problem102.levelOrder(treeNode1);
        System.out.println(Arrays.asList(lists));
    }
}
